package com.zxcrm.zxcrm.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zxcrm.common.utils.DateUtils;
import com.zxcrm.common.utils.SecurityUtils;
import com.zxcrm.zxcrm.domain.ZxcrmPlanKnowPerson;
import com.zxcrm.zxcrm.domain.ZxcrmPlanParty;
import com.zxcrm.zxcrm.mapper.ZxcrmPlanKnowPersonMapper;
import com.zxcrm.zxcrm.mapper.ZxcrmPlanPartyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zxcrm.zxcrm.mapper.ZxcrmPlanMapper;
import com.zxcrm.zxcrm.domain.ZxcrmPlan;
import com.zxcrm.zxcrm.service.IZxcrmPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 计划Service业务层处理
 * 
 * @author xu
 * @date 2025-07-08
 */
@Service
public class ZxcrmPlanServiceImpl implements IZxcrmPlanService 
{
    @Autowired
    private ZxcrmPlanMapper zxcrmPlanMapper;

    @Autowired
    private ZxcrmPlanPartyMapper zxcrmPlanPartyMapper;

    @Autowired
    private ZxcrmPlanKnowPersonMapper zxcrmPlanKnowPersonMapper;

    /**
     * 查询计划
     * 
     * @param id 计划主键
     * @return 计划
     */
    @Override
    public Map<String, Object> selectZxcrmPlanById(Long id)
    {
        Map<String, Object> resultMap = zxcrmPlanMapper.selectZxcrmPlanById(id);
        if (resultMap.get("planParties") != null) {
            String[] participants = resultMap.get("planParties").toString().split(",");
            resultMap.put("planParties", participants);
        }
        if (resultMap.get("observers") != null) {
            String[] observers = resultMap.get("observers").toString().split(",");
            resultMap.put("observers", observers);
        }
        return resultMap;
    }

    /**
     * 查询计划列表
     * 
     * @param zxcrmPlan 计划
     * @return 计划
     */
    @Override
    public List<ZxcrmPlan> selectZxcrmPlanList(ZxcrmPlan zxcrmPlan)
    {
        return zxcrmPlanMapper.selectZxcrmPlanList(zxcrmPlan);
    }

    /**
     * 新增计划
     * 
     * @param zxcrmPlan 计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertZxcrmPlan(ZxcrmPlan zxcrmPlan)
    {
        zxcrmPlan.setStatus("0");
        zxcrmPlan.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        zxcrmPlan.setCreateTime(DateUtils.getNowDate());

        //计划参与人
        List<ZxcrmPlanParty> planParties = new ArrayList<>();
        for (Long userId : zxcrmPlan.getPlanParties()) {
            ZxcrmPlanParty planParty = new ZxcrmPlanParty(SecurityUtils.getUserId(), userId);
            planParties.add(planParty);
        }
        zxcrmPlanPartyMapper.batchInsert(planParties);

        //计划知晓人
        List<ZxcrmPlanKnowPerson> knowPersonList = new ArrayList<>();
        for (Long userId : zxcrmPlan.getKnowPersons()) {
            ZxcrmPlanKnowPerson knowPerson = new ZxcrmPlanKnowPerson(SecurityUtils.getUserId(), userId);
            knowPersonList.add(knowPerson);
        }
        zxcrmPlanKnowPersonMapper.batchInsert(knowPersonList);

        return zxcrmPlanMapper.insertZxcrmPlan(zxcrmPlan);
    }

    /**
     * 修改计划
     * 
     * @param zxcrmPlan 计划
     * @return 结果
     */
    @Override
    public int updateZxcrmPlan(ZxcrmPlan zxcrmPlan)
    {
        zxcrmPlan.setUpdateTime(DateUtils.getNowDate());
        return zxcrmPlanMapper.updateZxcrmPlan(zxcrmPlan);
    }

    /**
     * 批量删除计划
     * 
     * @param ids 需要删除的计划主键
     * @return 结果
     */
    @Override
    public int deleteZxcrmPlanByIds(Long[] ids)
    {
        return zxcrmPlanMapper.deleteZxcrmPlanByIds(ids);
    }

    /**
     * 删除计划信息
     * 
     * @param id 计划主键
     * @return 结果
     */
    @Override
    public int deleteZxcrmPlanById(Long id)
    {
        return zxcrmPlanMapper.deleteZxcrmPlanById(id);
    }

    @Override
    public List<Map<String, Object> > selectZxcrmMyPlanList(ZxcrmPlan zxcrmPlan) {
        //计划创建者、计划参与者
        zxcrmPlan.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return zxcrmPlanMapper.selectZxcrmMyPlanList();
    }

    @Override
    public List<Map<String, Object>>  selectZxcrmTeamPlanList(ZxcrmPlan zxcrmPlan) {
        //计划创建者、计划参与者、计划知晓者
        return zxcrmPlanMapper.selectZxcrmTeamPlanList();
    }
}
